<template>
    <v-dialog v-model="dialogFormVisible" max-width="800" persistent>
        <v-card>
            <v-card-title>{{ title }}</v-card-title>
            <v-card-text>
                <v-form ref="formRef" v-model="formValid" :disabled="loading">
                    <v-row>
                        <v-col cols="12" md="6">
                            <v-text-field v-model="form.codeTitle" label="代码标题" :rules="rules.codeTitle" required />
                        </v-col>
                        <v-col cols="12" md="6">
                            <v-text-field v-model="form.codeTitleBrief" label="标题简介" :rules="rules.codeTitleBrief"
                                required />
                        </v-col>
                        <v-col cols="12" md="6">
                            <v-autocomplete v-model="form.packageName" :items="pathRestaurants.map((i) => i.value)"
                                label="包路径" :rules="rules.packageName" clearable />
                        </v-col>
                        <v-col cols="12" md="6">
                            <v-text-field v-model="form.moduleName" label="模块名" :rules="rules.moduleName" required />
                        </v-col>
                        <v-col cols="12" md="6">
                            <v-text-field v-model="form.subModuleName" label="子模块名" :rules="rules.subModuleName" />
                        </v-col>
                        <v-col cols="12" md="6">
                            <v-text-field v-model="form.authorName" label="作者信息" :rules="rules.authorName" required />
                        </v-col>
                        <v-col cols="12" md="6">
                            <v-select v-model="form.templateId" :items="templateList" item-title="tempName"
                                item-value="id" label="选择模板" :rules="rules.templateId" required />
                        </v-col>
                    </v-row>
                </v-form>
            </v-card-text>
            <v-card-actions>
                <v-spacer />
                <v-btn text @click="close">取 消</v-btn>
                <v-btn color="primary" :loading="loading" @click="save">确 定</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue';
import { getGenLogs, doCreate } from '@/api/generator/tableManagement';
import { getList } from '@/api/generator/template/GenTemplateManagement';
import { isNotNull } from '@/utils/validate';
import { validatorRule } from '@/utils/validateRule';

const dialogFormVisible = ref(false);
const title = ref('');
const formValid = ref(true);
const loading = ref(false);
const formRef = ref();
const templateList = ref<any[]>([]);
const pathRestaurants = [{ value: 'cn.it58.goodclass' }, { value: 'org.' }, { value: 'org.opsli' }, { value: 'org.opsli.modulars' }, { value: 'com.' }];
const form = reactive({
    id: '',
    tableId: '',
    tableType: '1',
    codeTitle: '',
    codeTitleBrief: '',
    packageName: '',
    moduleName: '',
    subModuleName: '',
    authorName: 'guagua',
    templateId: 0
});
const rules = {
    codeTitle: [
        (v: string) => !!v || '请输入代码标题',
        (v: string) => validatorRule.IS_GENERAL_WITH_CHINESE(v) || '只能为汉字、字母、数字和下划线'
    ],
    codeTitleBrief: [
        (v: string) => !!v || '请输入代码标题简介',
        (v: string) => validatorRule.IS_GENERAL_WITH_CHINESE(v) || '只能为汉字、字母、数字和下划线'
    ],
    packageName: [(v: string) => !!v || '请输入包路径'],
    moduleName: [(v: string) => !!v || '请输入模块名', (v: string) => /^[a-zA-Z]+$/.test(v) || '只能为字母'],
    subModuleName: [(v: string) => !v || /^[a-zA-Z]+$/.test(v) || '只能为字母'],
    authorName: [
        (v: string) => !!v || '请输入作者信息',
        (v: string) => validatorRule.IS_GENERAL_WITH_CHINESE(v) || '只能为汉字、字母、数字和下划线'
    ],
    templateId: [(v: string) => !!v || '请选择模板']
};

function open(row?: any) {
    if (row) {
        title.value = '生成代码';
        form.tableId = row.id;
        form.tableType = row.tableType;
        fetchData();
    } else {
        title.value = '生成代码';
        Object.assign(form, {
            id: '',
            tableId: '',
            tableType: '',
            codeTitle: '',
            codeTitleBrief: '',
            packageName: '',
            moduleName: '',
            subModuleName: '',
            authorName: '17607080935',
            templateId: "0"
        });
    }
    dialogFormVisible.value = true;
}
function close() {
    dialogFormVisible.value = false;
    nextTick(() => {
        if (formRef.value) (formRef.value as any).reset();
        Object.assign(form, {
            id: '',
            tableId: '',
            tableType: '',
            codeTitle: '',
            codeTitleBrief: '',
            packageName: '',
            moduleName: '',
            subModuleName: '',
            authorName: '17607080935',
            templateId: ''
        });
    });
}
function save() {
    if (!formRef.value) return;
    (formRef.value as any).validate().then(async (valid: boolean) => {
        if (valid) {
            loading.value = true;
            await doCreate(form);
            loading.value = false;
            close();
        }
    });
}
async function fetchData() {
    loading.value = true;
    if (form.tableId) {
        const { data } = await getGenLogs({ tableId: form.tableId });
        console.log(data);
        if (isNotNull(data)) {
            Object.assign(form, data);
        }
    }
    console.log(form.tableType);
    if (form.tableType) {
        const { data } = await getList({ tableType_EQ: form.tableType });
        if (isNotNull(data)) {
            templateList.value = data.rows;
        }
    }
    loading.value = false;
}

defineExpose({ showEdit: open });
</script>
